//
//  CollectionViewController.m
//  InSuger
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "CollectionViewController.h"

@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation CollectionViewController

- (void)viewWillAppear:(BOOL)animated {
    
    //[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    self.tableArray = [[DataBaseHandle shareDataBaseHandle] selectAllModel];
    
    if (self.tableArray.count == 0) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height)];
        [self.tableView addSubview:imageView];
        imageView.image = [UIImage imageNamed:@"collect.png"];
        
        [AlertController createAlertTitle:@"提示信息" alertMessage:@"暂无收藏" cancelTitle:@"取消" doneTitle:@"确认" viewController:self block:^(id block) {
            
            
        }];
    }
    [self.tableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self createTableView];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    [[DataBaseHandle shareDataBaseHandle] openDB];
    self.tableArray = [NSMutableArray array];
    self.tableArray = [[DataBaseHandle shareDataBaseHandle] selectAllModel];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tableArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    HomePageTableViewModel *model = self.tableArray[indexPath.row];
    
    cell.textLabel.text = model.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageDetailViewController *detailVC = [[HomePageDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.model = self.tableArray[indexPath.row];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
